package com.qc.wintrax.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qc.wintrax.BaseActivity;
import com.qc.wintrax.R;
import com.qc.wintrax.fragment.HistoryFragment;
import com.qc.wintrax.fragment.LineFragment;


import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LineToCollectActivity extends BaseActivity {
    @Bind(R.id.fl_content)
    FrameLayout flContent;
    @Bind(R.id.id_tab_line_img)
    ImageButton idTabLineImg;
    @Bind(R.id.caiji_txt)
    TextView caijiTxt;
    @Bind(R.id.id_tab_line)
    LinearLayout idTabLine;
    @Bind(R.id.id_tab_lishi_img)
    ImageButton idTabLishiImg;
    @Bind(R.id.lishi_txt)
    TextView lishiTxt;
    @Bind(R.id.id_tab_history)
    LinearLayout idTabHistory;

    public LineFragment lineFragment;
    public HistoryFragment historyFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_to_collect);
        ButterKnife.bind(this);
        initFragment(0);
    }

    public void initFragment(int index) {
        // 由于是引用了V4包下的Fragment，所以这里的管理器要用getSupportFragmentManager获取
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 隐藏所有Fragment
        hideFragment(transaction);
        switch (index) {
            case 0:
                idTabLineImg.setImageResource(R.mipmap.caiji2);
                caijiTxt.setTextColor(getResources().getColor(R.color.lvse2));
                if (lineFragment == null) {
                    lineFragment = new LineFragment();
                    transaction.add(R.id.fl_content, lineFragment);
                } else {
                    transaction.show(lineFragment);
                }

                break;
            case 1:
                idTabLineImg.setImageResource(R.mipmap.caiji);
                caijiTxt.setTextColor(getResources().getColor(R.color.huise));
                idTabLishiImg.setImageResource(R.mipmap.lishi2);
                lishiTxt.setTextColor(getResources().getColor(R.color.lvse2));
                if (historyFragment == null) {
                    historyFragment = new HistoryFragment();
                    transaction.add(R.id.fl_content, historyFragment);
                } else {
                    transaction.show(historyFragment);
                }
                break;
            default:
                break;
        }
        // 提交事务
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (lineFragment != null) {
            transaction.hide(lineFragment);
        }
        if (historyFragment != null) {
            transaction.hide(historyFragment);
        }
    }

    private void restartBotton() {
        // ImageView置为灰色
        idTabLineImg.setImageResource(R.mipmap.caiji);
        idTabLishiImg.setImageResource(R.mipmap.lishi);
        // TextView置为白色
        caijiTxt.setTextColor(getResources().getColor(R.color.huise));
        lishiTxt.setTextColor(getResources().getColor(R.color.huise));
    }

    @OnClick({R.id.id_tab_line, R.id.id_tab_history})
    public void onClick(View view) {
        // 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色，然后根据点击着色
        restartBotton();
        // ImageView和TetxView置为绿色，页面随之跳转
        switch (view.getId()) {
            case R.id.id_tab_line:
                initFragment(0);
                break;
            case R.id.id_tab_history:
                initFragment(1);
                break;
            default:
                break;
        }
    }
}
